﻿using GN.Pay.Core.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GN.Pay.Core.Values.Services;
using GN.Framework.Utils;
using GN.Framework.Extensions;

namespace GN.Pay.Application.Service
{
    /// <summary>
    /// 聚合银行卡查询服务
    /// </summary>
    public class JuheBankCardQueryService : JuheService, IBankCardQueryService
    {
        /// <summary>
        /// 
        /// </summary>
        protected override string AppKeyName
        {
            get
            {
                return "JuheBankCardVerifyAppKey";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cardNo"></param>
        /// <returns></returns>
        public IBankCardAscription AscriptionQuery(string cardNo)
        {
            throw new NotImplementedException();
        }

        private const string ServiceUrl = "http://v.juhe.cn/verifybankcard4/query";

        private class BankCardVerifyMessageResult
        {
            public string Jobid { get; set; }

            public string Realname { get; set; }

            public string Bankcard { get; set; }

            public string Idcard { get; set; }

            public string Mobile { get; set; }

            public string Res { get; set; }

            public string Message { get; set; }
        }

        private class BankCardVerifyMessage
        {
            public string Reason { get; set; }

            public string Error_code { get; set; }

            public BankCardVerifyMessageResult Result { get; set; }

            /// <summary>
            /// 是否成功
            /// </summary>
            /// <returns></returns>
            public bool IsSuccess()
            {
                return "0".Equals(Error_code) && Result != null && "1".Equals(Result.Res);
            }

            public string StateMessage()
            {
                if (IsSuccess())
                {
                    return "成功匹配";
                }
                if (string.IsNullOrWhiteSpace(Reason))
                {
                    return Reason;
                }
                if (Result != null && !string.IsNullOrWhiteSpace(Result.Message))
                {
                    return Result.Message;
                }
                return "不匹配";
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="verifyInfo"></param>
        /// <returns></returns>
        public IBankCardVerifyResult Verify(IBankCardVerify verifyInfo)
        {
            try
            {
                var parameters = new Dictionary<string, string>();
                parameters["key"] = AppKey;
                parameters["realname"] = verifyInfo.RealName;
                parameters["idcard"] = verifyInfo.IdentityNo;
                parameters["bankcard"] = verifyInfo.CardNo;
                parameters["mobile"] = verifyInfo.MobilePhone;
                string result = HttpUtils.PostString(ServiceUrl, parameters);
                var msg = result.JsonToObject<BankCardVerifyMessage>(true);
                return new BankCardVerifyResult() { IsSuccess = msg.IsSuccess(), StateMessage = msg.StateMessage() };
            }
            catch (Exception err)
            {
                return new BankCardVerifyResult() { IsSuccess = false, StateMessage = err.Message };
            }
        }
    }
}
